package com.gsafety.wellsfort.controller.course;

import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.ResultCode;
import com.gsafety.wellsfort.domain.dto.course.*;
import com.gsafety.wellsfort.domain.dto.ids.CourseDto;
import com.gsafety.wellsfort.domain.vo.CourseDetailVO;
import com.gsafety.wellsfort.domain.vo.CourseRangeVO;
import com.gsafety.wellsfort.domain.vo.CourseVO;
import com.gsafety.wellsfort.domain.vo.LessonPeriodVO;
import com.gsafety.wellsfort.service.course.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "课程管理", description = "课程管理接口")
@RestController
@Slf4j
@RequestMapping("/api/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "保存课程基本信息")
    @PostMapping("/save-baseinfo")
    public ApiResponse saveBaseinfo(@RequestBody CourseDTO qDto) throws Exception {
        return courseService.saveBaseInfo(qDto);
    }

    @ApiOperation(value = "保存课程范围")
    @PostMapping("/save-crange")
    public ApiResponse saveCourseRange(@RequestBody CourseRangeVO courseRangeVO) {
        if (courseRangeVO != null) {
            return courseService.saveCourseRange(courseRangeVO);
        }
        return ApiResponse.error(ResultCode.INVALID_PARAM);
    }

    @ApiOperation(value = "删除课程")
    @PostMapping("/remove-course")
    public ApiResponse deleteCourse(@RequestBody CourseDto course) {
        return courseService.deleteByCourseId(course.getCourseId());
    }

    @ApiOperation(value = "编辑/详情/预览课程")
    @PostMapping("/detail-course")
    public ApiResponse<CourseDetailVO> detailCourse(@RequestBody CourseDto courseDto) {
        return courseService.getCourseDetail(courseDto.getCourseId());
    }

    @ApiOperation(value = "查询课程列表")
    @PostMapping("/course-list")
    public ApiResponse<List<CourseVO>> findByPageParams(@RequestBody CourseQueryDTO queryParams) {
        return courseService.findByPageParams(queryParams);
    }

    /***
     * 业务逻辑:
     * 添加课时时候,同时生成 （1)作业和试卷的发布记录 (2) 添加学生作业和考试记录 (3)每个学生的课时课件学习进度
     * 更新课时时,先删除,再重新添加
     * @param lessonPeriodDTO
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "给课程添加课时")
    @PostMapping("/add-lessonPeriod")
    public ApiResponse<LessonPeriodDTO> addLessonPeriod(@RequestBody LessonPeriodDTO lessonPeriodDTO) throws Exception {
        return courseService.addOrUpdateLessonPeriod(lessonPeriodDTO);
    }

    @ApiOperation(value = "课时取消发布")
    @PostMapping("/unPublish-lessonPeriod")
    public ApiResponse unPublishLessonPeriod(@RequestBody UnPublishLessonPeriodDTO unPublishLessonPeriodDTO) throws Exception {
        return courseService.unPublishLessonPeriod(unPublishLessonPeriodDTO);
    }

    @ApiOperation(value = "删除课时")
    @PostMapping("/remove-lessonPeriod")
    public ApiResponse removeLessonPeriod(@RequestBody CourseDto courseDto) {
        return courseService.deleteLessonPeriod(courseDto.getLessonPeriodId());
    }

    @ApiOperation(value = "发布课时作业")
    @PostMapping("/publish-lessonPeriWk")
    public ApiResponse<WorkPublishDTO> publishLessonPeriodWork(@RequestBody WorkPublishDTO workPublishDTO) {
        return courseService.publishLessonPeriodWork(workPublishDTO);
    }

    @ApiOperation(value = "获取课时详情")
    @PostMapping("/get-lessonPeriod")
    public ApiResponse<LessonPeriodVO> getLessonPeriod(@RequestBody LessonPeriodDetailDTO lessonPeriodDetailDTO) {
        return courseService.getLessonPeriodAdmin(lessonPeriodDetailDTO);
    }

    @ApiOperation(value = "获取课时列表")
    @PostMapping("/get-lessonPeriodList")
    public ApiResponse<LessonPeriodVO> getLessonPeriodList(@RequestBody CourseDto courseDto) {
        return courseService.getLessonPeriodList(courseDto);
    }
}
